<template>
  <view class="preview-container" @click="toggleMask">
    <swiper 
      class="image-swiper"
      :autoplay="true"
      :interval="3000"
      :circular="true"
      :indicator-dots="false"
      @change="onSwiperChange"
    >
      <swiper-item v-for="(item, index) in 5" :key="index">
        <image
          src="/static/preview1.jpg" 
          mode="aspectFill" 
          class="swiper-image"
          @error="handleImageError"
        />
      </swiper-item>
    </swiper>
    
    <view class="mask" v-if="showMask" :class="{ 'mask-hidden': !maskState }">
      <view class="go-back" @click.stop="handleGoBack">
        <uni-icons type="arrowleft" size="28" color="#ffffff"></uni-icons>
      </view>
      
      <view class="count">{{ currentIndex + 1 }}/5</view>
      
      <view class="time-date">
        <text class="time">{{ formattedTime }}</text>
        <text class="date">{{ formattedDate }}</text>
      </view>
      
      <view class="footer">
        <view class="box" @click.stop="handleInfo">
          <uni-icons type="info" size="28" :color="iconColor"></uni-icons>
          <view class="text">信息</view>
        </view>
        <view class="box" @click.stop="handleRate">
          <uni-icons type="star-filled" size="28" :color="iconColor"></uni-icons>
          <view class="text">{{ userScore || '评分' }}</view>
        </view>
        <view class="box" @click.stop="handleDownload">
          <uni-icons type="download-filled" size="28" :color="iconColor"></uni-icons>
          <view class="text">下载</view>
        </view>
      </view>
    </view>

    <uni-popup ref="infoPopup" type="bottom">
      <view class="info-popup">
        <view class="popup-header">
          <view class="title">壁纸信息</view>
          <view class="close" @click="closeInfoPopup">
            <uni-icons type="closeempty" size="18" color="#999"></uni-icons>
          </view>
        </view>
        <scroll-view scroll-y class="popup-content">
          <view class="info-section">
            <view class="info-row">
              <text class="info-label">壁纸ID：</text>
              <text class="info-value" selectable>12312312adfa</text>
            </view>
            
            <view class="info-row">
              <text class="info-label">分类：</text>
              <text class="info-value">明星美女</text>
            </view>
            
            <view class="info-row">
              <text class="info-label">发布者：</text>
              <text class="info-value">威虾米</text>
            </view>
          </view>
          
          <view class="divider"></view>
          
          <view class="summary-section">
            <text class="summary-text">摘要文字内容填充部分，摘要文字内容填充部分，摘要文字内容填充部分，摘要文字内容填充部分。</text>
          </view>
          
          <view class="divider"></view>
          
          <view class="tags-section">
            <text class="section-title">标签：</text>
            <view class="tags-container">
              <text class="tag">标签名</text>
              <text class="tag">标签名</text>
              <text class="tag">标签名</text>
            </view>
          </view>
          
          <view class="copyright">
            声明：本图片由用户投稿，非商业使用，
            用于免费学习交流。如侵犯您的权益，
            请拷贝壁纸ID举报至平台：513894357@qq.com，
            我们将及时处理。
          </view>
        </scroll-view>
      </view>
    </uni-popup>

    <uni-popup ref="scorePopup" type="center">
      <view class="score-popup">
        <view class="pop-header">
          <view class="title">壁纸评分</view>
          <view class="close" @click="clickScoreClose">
            <uni-icons type="closeempty" size="18" color="#999"></uni-icons>
          </view>
        </view>
        <view class="content">
          <uni-rate v-model="userScore" />
          <text class="text">{{userScore}}分</text>
        </view>
        <view class="footer">
          <button :disabled="!userScore" type="default" size="mini" plain @click="submitRating">确认评分</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';

// 显示控制
const maskState = ref(true);
const showMask = ref(true);
const toggleMask = () => {
  maskState.value = !maskState.value;
  if (!maskState.value) {
    setTimeout(() => showMask.value = false, 300);
  } else {
    showMask.value = true;
  }
};

// 实时时间
const currentTime = ref(new Date());
let timer = null;

const updateTime = () => {
  currentTime.value = new Date();
};

onMounted(() => {
  updateTime();
  timer = setInterval(updateTime, 60000); // 每分钟更新一次
});

onUnmounted(() => {
  clearInterval(timer);
});

// 格式化时间
const formattedTime = computed(() => {
  const hours = currentTime.value.getHours().toString().padStart(2, '0');
  const minutes = currentTime.value.getMinutes().toString().padStart(2, '0');
  return `${hours}:${minutes}`;
});

const formattedDate = computed(() => {
  const month = (currentTime.value.getMonth() + 1).toString().padStart(2, '0');
  const day = currentTime.value.getDate().toString().padStart(2, '0');
  return `${month}月${day}日`;
});

// 图标颜色
const iconColor = ref('#ffffff');

// 当前图片索引
const currentIndex = ref(0);
const onSwiperChange = (e) => {
  currentIndex.value = e.detail.current;
};

// 弹出层引用
const infoPopup = ref(null);
const scorePopup = ref(null);
const userScore = ref(0);

// 按钮事件
const handleGoBack = (e) => {
  e.stopPropagation();
  uni.navigateBack();
};

const handleInfo = (e) => {
  e.stopPropagation();
  infoPopup.value.open();
};

const handleRate = (e) => {
  e.stopPropagation();
  scorePopup.value.open();
};

const handleDownload = (e) => {
  e.stopPropagation();
  uni.showLoading({ title: '下载中...' });
  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({ title: '下载成功', icon: 'success' });
  }, 1500);
};

const closeInfoPopup = () => {
  infoPopup.value.close();
};

const clickScoreClose = () => {
  scorePopup.value.close();
};

const submitRating = () => {
  uni.showToast({ 
    title: `感谢您的${userScore.value}星评分`,
    icon: 'none'
  });
  scorePopup.value.close();
};

const handleImageError = (e) => {
  console.error('图片加载失败:', e);
  uni.showToast({ title: '图片加载失败', icon: 'none' });
};
</script>

<style lang="scss" scoped>
@import '@/common/style/base-style.scss';

.preview-container {
  width: 100%;
  height: 100vh;
  position: relative;
  
  .image-swiper {
    width: 100%;
    height: 100%;
    
    .swiper-image {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transition: all 0.3s ease;
    opacity: 1;
    will-change: opacity;
    
    &.mask-hidden {
      opacity: 0;
      pointer-events: none;
    
      /* 只隐藏其他元素，不隐藏返回按钮 */
      .count,
      .time-date,
      .footer {
        transform: translateY(20px);
        opacity: 0;
      }
    
      /* 返回按钮保持可见 */
      .go-back {
        transform: none !important;
        opacity: 1 !important;
      }
    }
  
    .footer .box {
      ::v-deep .uni-icons {
        color: v-bind(iconColor) !important;
        font-size: 28rpx !important;
        display: block !important;
      }
    }

   .go-back {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 30rpx); /* 适配刘海屏 */
  left: 30rpx;
  width: 60rpx;
  height: 60rpx;
  transform: none !important; /* 强制覆盖遮罩隐藏的 transform */
  opacity: 1 !important; /* 强制覆盖遮罩隐藏的 opacity */
  background: rgba(0,0,0,0.5); /* 半黑背景确保可见性 */
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100; /* 确保置于顶层 */
  transition: all 0.3s ease;
  
  /* 图标样式 */
  ::v-deep .uni-icons {
    color: #fff !important; /* 强制白色图标 */
    font-size: 28rpx !important; /* 强制设置图标大小 */
  }
}

    
    .count {
      position: absolute;
      top: 10vh;
      left: 0;
      right: 0;
      margin: auto;
      width: fit-content;
      background: rgba(0,0,0,0.3);
      font-size: 28rpx;
      color: #fff;
      border-radius: 40rpx;
      padding: 8rpx 28rpx;
      backdrop-filter: blur(10rpx);
      text-align: center;
      transition: all 0.3s ease;
    }
    
    .time-date {
      position: absolute;
      top: calc(10vh + 80rpx);
      left: 0;
      right: 0;
      text-align: center;
      transition: all 0.3s ease;
      
      .time {
        font-size: 140rpx;
        font-weight: 100;
        line-height: 1em;
        color: #fff;
        text-shadow: 0 4rpx rgba(0,0,0,0.3);
        display: block;
      }
      
      .date {
        font-size: 34rpx;
        color: #fff;
        text-shadow: 0 2rpx rgba(0,0,0,0.3);
        margin-top: 10rpx;
      }
    }
    
    .footer {
      position: absolute;
      bottom: 10vh;
      left: 50%;
      transform: translateX(-50%);
      width: 65vw;
      height: 120rpx;
      background: rgba(0,0,0,0.7);
      border-radius: 120rpx;
      display: flex;
      align-items: center;
      justify-content: space-around;
      box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.2);
      backdrop-filter: blur(20rpx);
      padding: 0 30rpx;
      
      .box {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        min-width: 80rpx;
        
        .text {
          color: #fff;
          font-size: 24rpx;
          margin-top: 8rpx;
        }
      }
    }
  }

  .info-popup {
    background: #fff;
    border-radius: 30rpx 30rpx 0 0;
    max-height: 70vh;
    padding: 30rpx;
    
    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 20rpx;
      border-bottom: 1rpx solid #f5f5f5;
      margin-bottom: 20rpx;
      
      .title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }
      
      .close {
        padding: 10rpx;
      }
    }
    
    .popup-content {
      max-height: 60vh;
      padding: 0 20rpx;
      
      .info-section {
        margin-bottom: 20rpx;
        
        .info-row {
          display: flex;
          margin-bottom: 16rpx;
          font-size: 28rpx;
          line-height: 1.5;
          
          .info-label {
            color: #666;
            width: 140rpx;
            text-align: right;
            margin-right: 20rpx;
          }
          
          .info-value {
            color: #333;
            flex: 1;
          }
        }
      }
      
      .divider {
        height: 1rpx;
        background-color: #f5f5f5;
        margin: 24rpx 0;
      }
      
      .summary-section {
        margin-bottom: 20rpx;
        
        .summary-text {
          font-size: 28rpx;
          color: #333;
          line-height: 1.6;
        }
      }
      
      .tags-section {
        .section-title {
          font-size: 28rpx;
          color: #666;
          display: block;
          margin-bottom: 12rpx;
        }
        
        .tags-container {
          display: flex;
          flex-wrap: wrap;
          
          .tag {
            background-color: lighten($brand-theme-color, 40%);
            color: $brand-theme-color;
            font-size: 24rpx;
            padding: 8rpx 20rpx;
            border-radius: 20rpx;
            margin-right: 12rpx;
            margin-bottom: 12rpx;
          }
        }
      }
      
      .copyright {
        font-size: 24rpx;
        color: $text-font-color-3;
        padding: 20rpx;
        background: #f6f6f6;
        border-radius: 10rpx;
        margin: 20rpx 0;
        line-height: 1.6;
      }
    }
  }

  .score-popup {
    background: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    width: 80vw;
    
    .pop-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30rpx;
      
      .title {
        font-size: 32rpx;
        font-weight: bold;
      }
      
      .close {
        padding: 5rpx;
      }
    }
    
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin: 30rpx 0;
      
      .text {
        margin-top: 20rpx;
        font-size: 28rpx;
        color: #333;
      }
    }
    
    .footer {
      display: flex;
      justify-content: center;
      margin-top: 20rpx;
      
      button {
        width: 200rpx;
        
        &[disabled] {
          opacity: 0.5;
        }
      }
    }
  }
}
</style>